/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.controller.setter;

import foxmob.mlts.component.FileManager;
import foxmob.mlts.model.QuestionPaper;
import foxmob.mlts.viewcontroller.setter.SetDefaultTestForm;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lunayo
 */
public class SetDefaultTestFormController {
    
    public static final String DEFAULT_TEST_PREFERENCE = "DefaultTestPreference";
    
    private SetDefaultTestForm delegate;
    
    public SetDefaultTestFormController(SetDefaultTestForm delegate) {
        this.delegate = delegate;
    }
    
    public void loadTestRecord() {
        FileManager manager = FileManager.getInstanceObject();
        ArrayList<QuestionPaper> records = manager.getTestRecord();
        delegate.getStudentTestList().setListData(records.toArray());
    }
    
    public void setDefaultTestRecord() {
        // save some variable to java properties
        FileManager fileManager = FileManager.getInstanceObject();
        String fileName = 
                delegate.getStudentTestList().getSelectedValue().toString();
        try {
            fileManager.addObjectToProperties(DEFAULT_TEST_PREFERENCE, fileName);
            delegate.setVisible(false);
        } catch (IOException ex) {
            Logger.getLogger(SetDefaultTestForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /*
     * Listener
     */
    public void studentTestListMouseDidClick(java.awt.event.MouseEvent evt) {
        if (evt.getClickCount() == 2) {
            setDefaultTestRecord();
        }
    }
    
    public void setButtonDidTap() {
        setDefaultTestRecord();
    }
}
